package org.jfsm.demo.ccounter.cnt;

import org.jfsm.core.ConcurrentFSM;
import org.jfsm.core.FSMOutput;
import org.jfsm.demo.ccounter.echo.EchoRequest;
import org.jfsm.demo.ccounter.echo.EchoResponse;
import org.jfsm.demo.ccounter.timer.TimerRequest;
import org.jfsm.demo.ccounter.timer.TimerResponse;

public class CntFSM extends ConcurrentFSM<CntContext, CntRequest, FSMOutput> {

    enum State {
        INIT, RUN, HOLD
    }

    public Reference<EchoRequest, EchoResponse> echo;
    public Reference<TimerRequest, TimerResponse> timer;

    @Override
    protected void build(TransitionMap tm) {
        // Handlers
        CntHandler hnd = new CntHandler(this);

        // Transition Map
        tm.setInitialState(State.INIT);

        tm.put(State.INIT, in.start, State.RUN, hnd.ehStart);

        tm.put(State.RUN, in.reset, State.RUN, hnd.ehReset);
        tm.put(State.RUN, in.sendPing, State.RUN, hnd.ehSendPing);
        tm.put(State.RUN, echo.out.pong, State.RUN, hnd.mhPong);
        tm.put(State.RUN, timer.out.timeout, State.HOLD, hnd.ehHold);

        tm.put(State.HOLD, timer.out.timeout, State.RUN, hnd.ehReset);
        tm.put(State.HOLD, echo.out.pong, State.HOLD);
    }

}
